package objects;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlcleaner.TagNode;

public class Release {
	
	Matcher _MTCH = null;
	
	/**
	 * Updated regex's, for the new filtering system.
	 * 
	 * Held in an array so they can more easily be iterated over.
	 */
	private static Pattern _SceneRegex = Pattern.compile("-[A-Za-z0-9]+\\z");
	
	private static Pattern[] _RLSParserPATTERNS = 
	{
		// 0 // Season & Episode Regex
	 Pattern.compile("[Ss][0-9]+[Ee][0-9]+")
		// 1 // Encoding Regex
	,Pattern.compile("((x264|X264|[hH][\\.]264)|(XviD|xvid)|(MPEG2)|(DiVX)|(DVDR)|(VC-1)|(WMV)|(BD))")
		// 2 // Media Source Regex
	,Pattern.compile("((HDTV|hdtv)|(PDTV)|(DVDRip|DVDRIP)|(Bluray|BluRay)|(BDRip|BRRip)|(DSR)|(TVRip)|(VHSRip)|(DVD5|DVD9|HDDVD)|(WEB[^\\-DLR]|WEB\\-DL|WEBRiP)|(BD5|BD9|BD25|BD50))")
		// 3 // Definition Regex
	,Pattern.compile("((SD)|(720.)|(1080.))")
		// 4 // Container Regex
	,Pattern.compile("(AVI|MKV|VOB|MPEG|MP4|ISO|WMV|[\\.]TS|M4V|M2TS)")
		// 5 // Audio Codec Regex
	,Pattern.compile("(DTS[^-]|DD5.1|AC3|DTS-HDMA|5[\\.]1)")
		// 6 // Season Only regex, for season packs
	,Pattern.compile("[Ss][0-9]+[^Ee\\.]")
	};
	
	// Raw release name, it isn't all that relevant when its just used to create the rest of 
	// the information.
	private String _RAWNAME  = "";
	
	// Release Name
	private StringBuffer _NAME = new StringBuffer("");
	
	// Release Type
	private StringBuffer _TYPE = new StringBuffer("");

	// Group Name, as its relevant to most things.
	private String _GRPNAME = "";

	// Variables Relevant to Media
	private String _SOURCE = "";
	private String _ENCODING = "";
	private String _AUDIOENCODING = "";
	private String _RESOLUTION = "";
	
	// Relevant to TV only
	private String _SEASON = "";
	private String _EPISODE = "";
	
	// Relevant to applications and some games.
	private String _VERSION = "";
	
	
	// Download Link to torrent, directly
	private String _DOWNLOADLINK = "";

	public Release() {
		
	}
	
	public Release(String RLSNAME, String DLLink) {
		_RAWNAME = RLSNAME;
		_DOWNLOADLINK = DLLink;

		dynamicReleaseCompiler();
	}
	
	private void dynamicReleaseCompiler(){
		String TMP = _RAWNAME;
		String T2 = null;
		int fIndex = TMP.length();
		
		// Initialize a matcher using TMP, since we're going 
		// to be manipulating it plenty
		_MTCH = _SceneRegex.matcher(TMP);
		
		// Find, and extract the group release name's tag; if it exists.
		if(_MTCH.usePattern(_SceneRegex).find()){
			_GRPNAME = TMP.substring(TMP.lastIndexOf("-") + 1);
			
			fIndex = TMP.lastIndexOf("-");
		}else{_GRPNAME = "NON-SCENE";}
		
		_MTCH = _SceneRegex.matcher(TMP);
		// Find, and extract the encoding type of the release, if it exists.
		if(_MTCH.usePattern(_RLSParserPATTERNS[1]).find()){
			_ENCODING = _MTCH.group();
			
			if(_MTCH.start() < fIndex){
				fIndex = _MTCH.start();
			}
		}
		
		_MTCH = _SceneRegex.matcher(TMP);
		// Find, and extract the audio encoding type of the release, if it exists.
		if(_MTCH.usePattern(_RLSParserPATTERNS[5]).find()){
			_AUDIOENCODING = _MTCH.group();
			
			if(_MTCH.start() < fIndex){
				fIndex = _MTCH.start();
			}
		}
		
		_MTCH = _SceneRegex.matcher(TMP);
		// Find, and extract the source of the release, if it exists.
		if(_MTCH.usePattern(_RLSParserPATTERNS[2]).find()){
			_SOURCE = _MTCH.group();
			
			if(_MTCH.start() < fIndex){
				fIndex = _MTCH.start();
			}
		}
		
		_MTCH = _SceneRegex.matcher(TMP);
		// Find, and extract the resolution of the release, if it exists.
		if(_MTCH.usePattern(_RLSParserPATTERNS[3]).find()){
			_RESOLUTION = _MTCH.group();
			
			if(_MTCH.start() < fIndex){
				fIndex = _MTCH.start();
			}
		}else{
			if(!_SOURCE.equals("")){
				_RESOLUTION = "Standard";
			}
		}
		
		_MTCH = _SceneRegex.matcher(TMP);
		// Find, and extract the season / episode
		if(_MTCH.usePattern(_RLSParserPATTERNS[0]).find()){
			T2 = _MTCH.group().toLowerCase();
			T2 = T2.replace("s", "");
			String[] TMPARR = T2.split("e");
			_SEASON = TMPARR[0];
			_EPISODE = TMPARR[1];
			
			if(_MTCH.start() < fIndex){
				fIndex = _MTCH.start();
			}
		}else{_SEASON = "None"; _EPISODE = "None";}

	
		_MTCH = _SceneRegex.matcher(TMP);
		// Find, and extract the season
		if(_MTCH.usePattern(_RLSParserPATTERNS[6]).find()){
			T2 = _MTCH.group().toLowerCase();
			T2 = T2.replace("s", "");
			
			_SEASON = T2;
			
			if(_MTCH.start() < fIndex){
				fIndex = _MTCH.start();
			}
		}
		
		if(!(_SEASON.equals("None") && _EPISODE.equals("None"))){
			_TYPE.append("TV");
		}
		
		if((_SEASON.equals("None") && _EPISODE.equals("None") 
				&& !_ENCODING.equals(""))){
			_TYPE.append("Movie");
		}
		
		if(!_SEASON.equals("None") && _EPISODE.equals("None")){
			_TYPE.append(" | SEASON");
		}
		if(!_SOURCE.equals("")){
			_TYPE.append(" | " + _RESOLUTION);
		}
		
		_NAME.append(TMP.substring(0, fIndex).replace(".", " "));
		
	}
	
	public void showRLS(){
		System.out.println("Raw Release Name: " + this._RAWNAME);
		System.out.println("Series Name // Movie Title: " + this._NAME.toString());
		System.out.println("Release Group: " + this._GRPNAME);
		System.out.println("Source of Release: " + this._SOURCE);
		System.out.println("Type: " + this._TYPE);
		System.out.println("Encoding: " + this._ENCODING);
		System.out.println("Resolution: " + this._RESOLUTION);
		System.out.println("Audio Encoding: " + this._AUDIOENCODING);
		System.out.println("Season: " + this._SEASON);
		System.out.println("Episode: " + this._EPISODE);
		System.out.println("Download Link: " + this._DOWNLOADLINK);
	}
	
	public String getName(){
		return _RAWNAME;
	}
}
